<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="1000px">
			<el-form ref="userDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="名称">
							<el-input v-model="state.ruleForm.name" placeholder="请输入病虫害名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="英文名称">
							<el-input v-model="state.ruleForm.ename" placeholder="请输入英文名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="迫害作物">
							<el-input v-model="state.ruleForm.plant" placeholder="请输入迫害作物" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="介绍">
							<el-input v-model="state.ruleForm.introduce" type="textarea" placeholder="请输入病虫害介绍" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="危害症状">
							<el-input v-model="state.ruleForm.symptom" type="textarea" placeholder="请输入危害症状" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="发生原因">
							<el-input v-model="state.ruleForm.cause" type="textarea" placeholder="请输入发生原因" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="防治办法">
							<el-input v-model="state.ruleForm.measure" type="textarea" placeholder="请输入防治办法" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="图片">
							<el-input v-model="state.ruleForm.picture" type="textarea" placeholder="请输入图片地址" clearable></el-input>
						</el-form-item>
					</el-col>
					
					
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="数据状态">
							<el-switch v-model="state.ruleForm.status" inline-prompt active-text="启" inactive-text="禁"></el-switch>
						</el-form-item>
					</el-col>
					
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="systemUserDialog">
import {ElMessage } from 'element-plus';
import axios from 'axios';
import { startCase } from 'lodash';
import api from "/@/api.js";
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
// 定义变量内容
const userDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		id: '',
		name:'',
		ename:'',
        plant:'',
        introduce:'',
        symptom:'',
        cause:'',
		measure:'',
		picture:'',
		status:true,
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type, row) => {
	//console.log(row)
	state.dialog.type = type;
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改数据';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增数据';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		// nextTick(() => {
		// 	userDialogFormRef.value.resetFields();
		// });
		state.ruleForm = {
			id: '',
			name:'',
			ename:'',
			plant:'',
			introduce:'',
			symptom:'',
			cause:'',
			measure:'',
			picture:'',
			status:true,
		}
	}
	state.dialog.isShowDialog = true;
	
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	

	if(state.dialog.type === 'add'){
		if(state.ruleForm.status){
			state.ruleForm.status = '1';
		}else{
			state.ruleForm.status = '0';
		}
		//alert(state.ruleForm.status);
		// 新增
		axios({
		method: 'post',
		url: api.commonUrl + '/diseaseData/addDiseaseData',
		data:state.ruleForm,
	}).then(function (response) {
		if(response.data.code == 200){
			
			ElMessage.success(response.data.message);
		}else{
			ElMessage.error(response.data.message);
			
		}
		emit('refresh');
	});
	}else{
		if(state.ruleForm.status){
			state.ruleForm.status = '1';
		}else{
			state.ruleForm.status = '0';
		}
		// 修改
		axios({
		method: 'post',
		url: api.commonUrl + '/diseaseData/updatediseadata',
		data:state.ruleForm,
	}).then(function (response) {
		if(response.data.code == 200){
			ElMessage.success(response.data.message);
		}else{
			ElMessage.error(response.data.message);
		}
		emit('refresh');
	});
	}
	
	closeDialog();
};


// 暴露变量
defineExpose({
	openDialog,
});
</script>
